<template>
  <div>
    <el-form ref="assetForm" :model="formItem" :rules="rules" label-width="120px" size="mini">
      <div class="anthor">
        <div class="anthor-info">
          <div id="baseInfo">
            <div class="anthor-info-head"><i class="el-icon-document" /><span>基本信息</span></div>
            <div class="anthor-info-body">
              <el-row :gutter="40">
                <el-col :span="8">
                  <el-form-item label="资产编号" prop="code">
                    <el-input v-model="formItem.code" placeholder="请输入资产编号" :maxlength="255">
                      <i slot="suffix" class="el-input__icon el-icon-question" title="编码必须是唯一的，如果不设置编码，系统将按照您设置的规则自动生成编码" />
                      <!-- </el-tooltip> -->
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="资产类型" prop="assetTypeCode">
                    <treeselect
                      v-model="formItem.assetTypeCode"
                      :multiple="false"
                      :options="typeOptions"
                      placeholder="选择资产类型"
                      :disable-branch-nodes="true"
                      @input="inputType"
                      @select="selectType"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="资产名称" prop="name">
                    <el-input v-model="formItem.name" placeholder="请输入名称" :maxlength="255" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="序列号">
                    <el-input v-model="formItem.sequence" placeholder="请输入资产条码" :maxlength="255" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="账套" prop="books">
                    <el-input v-model="formItem.books" placeholder="请输入账套" :maxlength="255" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="品牌">
                    <el-input v-model="formItem.brand" placeholder="品牌" :maxlength="255" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="规格型号">
                    <el-input v-model="formItem.specification" placeholder="规格型号" :maxlength="255" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="生产厂家">
                    <el-input v-model="formItem.produce" placeholder="生产厂家" :maxlength="255" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="用途">
                    <el-input v-model="formItem.purpose" placeholder="用途" :maxlength="255" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="原资产编号">
                    <el-input v-model="formItem.originCode" placeholder="原资产编号" :maxlength="255" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="数量" prop="quantity">
                    <el-input-number v-model="formItem.quantity" :step-strictly="true" :step="1" :max="1000000000" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="合同编号以及名称">
                    <el-input v-model="formItem.contractNameAndCode" placeholder="合同编号以及名称" :maxlength="255" />
                  </el-form-item>
                </el-col>

                <el-col :span="8">
                  <el-form-item label="维保截至时间">
                    <el-date-picker
                      v-model="formItem.manufacturerEndDate"
                      type="date"
                      placeholder="选择维保截至时间"
                      align="right"
                      format="yyyy-MM-dd"
                      value-format="yyyy-MM-dd"
                      :picker-options="pickerOptions"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="备用字段">
                    <el-input v-model="formItem.backup" placeholder="备用字段" :maxlength="255" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="是否在线">
                    <el-select v-model="formItem.running" filterable clearable class="select_input" placeholder="请选择是否在线">
                      <el-option v-for="(item) in isOperation" :key="item.code" :label="item.text" :value="item.code" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="备注">
                    <el-input v-model="formItem.remark" placeholder="备注" :maxlength="255" />
                  </el-form-item>
                </el-col>

              </el-row>
            </div>
          </div>
          <div id="financeInfo">
            <div class="anthor-info-head"><i class="el-icon-document" /><span>财务信息</span></div>
            <div class="anthor-info-body">
              <el-row>
                <el-col :span="8 " :gutter="40">
                  <el-form-item label="自有价值">
                    <el-input-number v-model="formItem.ownvalue" :precision="2" :step="1" :max="1000000000" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="价值类型" prop="valuetype">
                    <el-select v-model="formItem.valuetype" clearable filterable class="select_input" placeholder="请选择价值类型">
                      <el-option v-for="(item) in valuetypes" :key="item.code" :label="item.text" :value="item.code" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="价值" prop="money">
                    <el-input-number v-model="formItem.money" :precision="2" :step="1" :max="1000000000" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="折旧状态">
                    <el-input v-model="formItem.depreciationstatus" placeholder="请输入折旧状态" :maxlength="255" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="已折旧月数">
                    <el-input-number v-model="formItem.depreciationmonth" :step-strictly="true" :step="1" :max="1000000000" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="累计折旧">
                    <el-input-number v-model="formItem.depreciationtotal" :precision="2" :step="1" :max="1000000000" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="财政性资金">
                    <el-input-number v-model="formItem.fundsfinancial" :precision="2" :step="1" :max="1000000000" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="非财政性资金">
                    <el-input-number v-model="formItem.unfundsfinancial" :precision="2" :step="1" :max="1000000000" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="净值">
                    <el-input-number v-model="formItem.equity" :precision="2" :step="1" :max="1000000000" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="闲置价值">
                    <el-input-number v-model="formItem.idleValue" :precision="2" :step="1" :max="1000000000" />
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </div>
          <div id="useInfo">
            <div class="anthor-info-head"><i class="el-icon-document" /><span>使用信息</span></div>
            <div class="anthor-info-body">
              <el-row :span="8" :gutter="40">
                <el-col :span="8">
                  <el-form-item label="取得日期" prop="getdate">
                    <el-date-picker
                      v-model="formItem.getdate"
                      type="date"
                      placeholder="选择取得日期"
                      align="right"
                      format="yyyy-MM-dd"
                      value-format="yyyy-MM-dd"
                      :picker-options="pickerOptions"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="取得方式">
                    <el-select v-model="formItem.sourceDesc" filterable clearable class="select_input" placeholder="请选择取得方式">
                      <el-option v-for="(item) in assetSource" :key="item.code" :label="item.text" :value="item.code" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="所属单位">
                    <el-input v-model="formItem.belongTo" placeholder="所属单位" :maxlength="255" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="投入使用日期" prop="usedate">
                    <el-date-picker
                      v-model="formItem.usedate"
                      type="date"
                      placeholder="选择投入使用日期"
                      align="right"
                      format="yyyy-MM-dd"
                      value-format="yyyy-MM-dd"
                      :picker-options="pickerOptions"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="使用年限（月）">
                    <el-input-number v-model="formItem.month" :step-strictly="true" :step="1" :max="1000000000" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="使用方向">
                    <el-input v-model="formItem.usetype" placeholder="请输入使用方向" :maxlength="255" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="使用状况" prop="statusDesc">
                    <el-select v-model="formItem.statusDesc" filterable clearable class="select_input" placeholder="请选择资产状况">
                      <el-option v-for="(item) in assetStatus" :key="item.code" :label="item.text" :value="item.code" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="使用部门">
                    <el-select v-model="formItem.useDeptDesc" filterable clearable class="select_input" placeholder="请选择使用部门">
                      <el-option v-for="(item) in useDepts" :key="item.code" :label="item.text" :value="item.code" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="使用人">
                    <el-input v-model="formItem.userName" placeholder="请输入使用人" :maxlength="255" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="管理部门">
                    <el-select v-model="formItem.manageDeptDesc" filterable clearable class="select_input" placeholder="请选择管理部门">
                      <el-option v-for="(item) in useDepts" :key="item.code" :label="item.text" :value="item.code" />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </div>
          <div id="officeInfo">
            <div class="anthor-info-head"><i class="el-icon-document" /><span>厅局信息</span></div>
            <div class="anthor-info-body">
              <el-row :span="8" :gutter="40">
                <el-col :span="8">
                  <el-form-item label="移交厅局" prop="submmitTo">
                    <el-select v-model="formItem.submmitTo" clearable filterable class="select_input" placeholder="请选择移交单位">
                      <el-option v-for="(item) in dcomInstitutions" :key="item.name" :label="item.name" :value="item.name" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="原资产卡片单位">
                    <el-select v-model="formItem.secondDept" filterable clearable class="select_input" placeholder="请选择原资产卡片单位">
                      <el-option v-for="(item) in secondDepts" :key="item.code" :label="item.text" :value="item.code" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="厅局机房地址">
                    <el-input v-model="formItem.officeComputerRoomAddress" placeholder="厅局机房地址" :maxlength="255" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="厅局地址">
                    <el-input v-model="formItem.officeAddress" placeholder="厅局地址" :maxlength="255" />
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </div>
          <div id="transferInfo">
            <div class="anthor-info-head"><i class="el-icon-document" /><span>移交信息</span></div>
            <div class="anthor-info-body">
              <el-row :span="8" :gutter="40">
                <el-col :span="8">
                  <el-form-item label="存放地点">
                    <el-input v-model="formItem.located" placeholder="存放地点" :maxlength="255" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="文档齐全">
                    <el-select v-model="formItem.isDocumentAll" filterable clearable class="select_input" placeholder="请选择管理部门">
                      <el-option v-for="(item) in isOperation" :key="item.code" :label="item.text" :value="item.code" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="业务应用系统">
                    <el-input v-model="formItem.businessSystem" placeholder="对应支撑的业务应用系统" :maxlength="255" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="是否报废">
                    <el-select v-model="formItem.isScrapped" filterable clearable class="select_input" placeholder="请选择是否报废">
                      <el-option v-for="(item) in isOperation" :key="item.code" :label="item.text" :value="item.code" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="制单人">
                    <el-input v-model="formItem.creator" placeholder="制单人" :maxlength="255" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="制单时间">
                    <el-date-picker
                      v-model="formItem.handoverDate"
                      type="date"
                      placeholder="选择制单时间"
                      align="right"
                      format="yyyy-MM-dd"
                      value-format="yyyy-MM-dd"
                      :picker-options="pickerOptions"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </div>
        </div>
        <anchor type="unstrict" :xleft="1" :ytop="12" :title-list="anchorList" />
      </div>
    </el-form>
  </div>
</template>

<script>
import treeselect from '@neu/biz-core/components/TreeSelect'
import Anchor from '@neu/biz-core/components/Anchor/index'
import { create, update, detail, insititutions } from '@neu/dcam-ui/api/fixedAsset'
import { findByDictionaryType } from '@neu/dcam-ui/api/dictionary'
import { listEffective } from '@neu/dcam-ui/api/assetExtFieldConfig'
import { tree } from '@neu/dcam-ui/api/assetType'
import * as locationApi from '@neu/dcam-ui/api/location'
import * as organizations from '@neu/auth-ui/api/organization'
import { formatDate } from '@neu/biz-core/utils/utils'

// 定义一个表单原始项
const formItem = {
  assetTypeCode: null,
  name: '',
  books: '',
  usedate: '',
  getdate: '',
  ownvalue: '',
  code: '',
  depreciationstatus: '',
  depreciationmonth: '',
  depreciationtotal: '',
  month: '',
  valuetype: '',
  money: '',
  fundsfinancial: '',
  unfundsfinancial: '',
  equity: '',
  sourceDesc: '',
  usetype: '',
  statusDesc: '',
  useDeptDesc: '',
  manageDeptDesc: '',
  userName: '',
  quantity: 1,
  creator: '',
  belongTo: '',
  submmitTo: '',
  specification: '',
  located: '',
  originCode: '',
  brand: '',
  idleValue: '',
  handoverDate: '',
  backup: '',
  remark: '',
  purpose: '',
  produce: '',
  manufacturerEndDate: '',
  isDocumentAll: '',
  businessSystem: '',
  contractNameAndCode: '',
  sequence: '',
  isScrapped: '',
  officeComputerRoomAddress: '',
  officeAddress: '',

  locationCode: null,
  productId: '',
  productName: '',
  measureUnit: '',
  orgId: null,
  orgName: '',
  userId: '',
  manufacturerId: '',
  manufacturerName: '',
  manufacturerPerson: '',
  manufacturerPhone: '',
  maintenanceId: '',
  maintenanceStart: null,
  maintenanceEnd: null,
  maintenanceUserId: '',
  maintenanceUserName: '',
  maintenanceNote: '',
  contractCode: '',
  contractName: '',
  contract: {},
  createTime: '',
  updateTime: '',
  secondDept: '',
  thirdDept: '',
  ext1: '',
  ext1Array: [],
  ext2: '',
  ext2Array: [],
  ext3: '',
  ext3Array: [],
  ext4: '',
  ext4Array: [],
  ext5: '',
  ext5Array: [],
  running: null
}
export default {
  name: 'FixedAssetEditFormPage',
  components: {
    Anchor,
    treeselect
  },
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },

  data() {
    return {
      anchorList: [ // 锚点列表
        { id: 'baseInfo', label: '基本信息' },
        { id: 'financeInfo', label: '财务信息' },
        { id: 'useInfo', label: '使用信息' },
        { id: 'officeInfo', label: '厅局信息' },
        { id: 'transferInfo', label: '移交信息' }
      ],
      /** 规格型号disable */
      productNameDisabled: true,
      /** 是否显示产品信息 */
      productSelDialogFormVisible: false,
      isOperation: [{
        code: 'YES',
        text: '是'
      }, {
        code: 'NO',
        text: '否'
      }],
      /** 资产来源 */
      assetSource: [],
      /** 资产状态 */
      assetStatus: [],
      /** 价值类型 */
      valuetypes: [],
      /** 使用部门 */
      useDepts: [],
      /** 管理部门 */
      manageDepts: [],
      /** 有效扩展属性 */
      effictiveExtField: [],
      /** 是否显示合同信息 */
      contractFormVisible: false,
      /** 是否显示用户选择对话框 */
      userSelDialogFormVisible: false,
      /** 是否显示维保负责人的数据 */
      mainteanceSelDialogFormVisible: false,
      /** 维保信息对话框是否显示 */
      manucacturerSelect: false,
      /** 资产位置树 */
      availableLocations: [],
      /** 资产类型树 */
      typeOptions: [],
      /** 使用部门 */
      availableOganizations: [],
      secondDepts: [],
      thirdDepts: [],
      /** 是否初始化 */
      isUpdateInit: true,
      /** 行数 */
      rows: 2,
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date())
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24)
            picker.$emit('pick', date)
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', date)
          }
        }]
      },
      formItem: { ...formItem },
      // 表单验证规则
      rules: {
        code: [{ required: true, message: '资产编号不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '资产名称不能为空', trigger: 'blur' }],
        getdate: [{ required: true, message: '取得日期不能为空', trigger: 'blur' }],
        valuetype: [{ required: true, message: '价值类型不能为空', trigger: 'blur' }],
        money: [{ required: true, message: '价值不能为空', trigger: 'blur' }],
        statusDesc: [{ required: true, message: '使用状况不能为空', trigger: 'blur' }],
        quantity: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
        submmitTo: [{ required: true, message: '移交厅局不能为空', trigger: 'blur' }],
        assetTypeCode: [{ required: true, message: '资产类型不能为空', trigger: 'change' }],
        createTime: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }],
        updateTime: [{ required: true, message: '更新时间不能为空', trigger: 'blur' }]
      },
      dcomInstitutions: []
    }
  },
  computed: {
    moduleType: function() {
      if (this.id !== -1) {
        return 'editModule'
      } else {
        return 'createModule'
      }
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: {
      handler() {
        if (this.id) {
          this.fetch()
        }
      },
      immediate: true
    }
  },
  created() {
    tree().then(response => {
      const result = response.data
      this.typeOptions = result
    })
    locationApi.tree().then(response => {
      this.availableLocations = response.data
    })
    organizations.tree().then(response => {
      this.availableOganizations = [response.data]
    })
    findByDictionaryType('valueType').then(response => {
      this.valuetypes = response.data
    })
    findByDictionaryType('assetSource').then(response => {
      this.assetSource = response.data
    })
    findByDictionaryType('thirdDept').then(response => {
      this.thirdDepts = response.data
    })
    findByDictionaryType('secondDept').then(response => {
      this.secondDepts = response.data
    })
    findByDictionaryType('assetStatus').then(response => {
      this.assetStatus = response.data
    })
    findByDictionaryType('useDept').then(response => {
      this.useDepts = response.data
    })
    findByDictionaryType('manageDept').then(response => {
      this.manageDepts = response.data
    })
    listEffective().then(response => {
      this.effictiveExtField = response.data
    })
    insititutions().then(response => {
      const dcomInstitutionsArray = response
      this.dcomInstitutions = []
      if (dcomInstitutionsArray) {
        dcomInstitutionsArray.forEach((data, index) => {
          this.dcomInstitutions.push(data)
        })
      }
    })
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.isUpdateInit = false
          this.formItem = { ... response.data }
          if (this.formItem.contract === null) {
            this.formItem.contract = {}
          }

          this.formItem.ext1Array = this.formItem.ext1 && this.formItem.ext1.indexOf('[') !== -1 ? JSON.parse(this.formItem.ext1) : ''
          this.formItem.ext2Array = this.formItem.ext2 && this.formItem.ext2.indexOf('[') !== -1 ? JSON.parse(this.formItem.ext2) : ''
          this.formItem.ext3Array = this.formItem.ext3 && this.formItem.ext3.indexOf('[') !== -1 ? JSON.parse(this.formItem.ext3) : ''
          this.formItem.ext4Array = this.formItem.ext4 && this.formItem.ext4.indexOf('[') !== -1 ? JSON.parse(this.formItem.ext4) : ''
          this.formItem.ext5Array = this.formItem.ext5 && this.formItem.ext5.indexOf('[') !== -1 ? JSON.parse(this.formItem.ext5) : ''
        })
      } else {
        this.formItem = { ...formItem }
      }
    },
    selectType() {

    },
    inputType(value) {
      if (value === undefined) {
        this.formItem.productName = ''
        this.productNameDisabled = true
      } else {
        this.productNameDisabled = false
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      this.formItem.ext1 = this.formItem.ext1Array.length > 0 ? JSON.stringify(this.formItem.ext1Array) : this.formItem.ext1
      this.formItem.ext2 = this.formItem.ext2Array.length > 0 ? JSON.stringify(this.formItem.ext2Array) : this.formItem.ext2
      this.formItem.ext3 = this.formItem.ext3Array.length > 0 ? JSON.stringify(this.formItem.ext3Array) : this.formItem.ext3
      this.formItem.ext4 = this.formItem.ext4Array.length > 0 ? JSON.stringify(this.formItem.ext4Array) : this.formItem.ext4
      this.formItem.ext5 = this.formItem.ext5Array.length > 0 ? JSON.stringify(this.formItem.ext5Array) : this.formItem.ext5

      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.assetForm.validate((valid) => {
          if (valid) {
            if (formatDate(this.formItem.maintenanceStart) > formatDate(this.formItem.maintenanceEnd)) {
              return this.$message({
                showClose: true,
                message: '维保开始时间大于维保结束时间',
                type: 'error'
              })
            }
            if (this.formItem.money <= 0) {
              return this.$message({
                showClose: true,
                message: '价值必须大于0',
                type: 'error'
              })
            }
            if (this.formItem.quantity <= 0) {
              return this.$message({
                showClose: true,
                message: '数量必须大于0',
                type: 'error'
              })
            }
            if (updated) {
              resolve(update(this.id, this.formItem))
            } else {
              resolve(create(this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.assetForm.resetFields()
      this.formItem = { ...formItem }
    },
    /** 选择供货商 */
    handleIconClick() {
      this.manucacturerSelect = true
    },
    selectManufaturerCancel() {
      this.manucacturerSelect = false
    },
    selectManufaturerOk() {
      var manufacturers = this.$refs.manfacturer.selections
      if (manufacturers.length > 1) {
        return this.$message({
          showClose: true,
          message: '只能选择一个供应商',
          type: 'error'
        })
      }
      if (manufacturers.length < 1) {
        this.formItem.manufacturerId = ''
        this.formItem.manufacturerName = ''
        this.formItem.manufacturerPerson = ''
        this.formItem.manufacturerPhone = ''
      } else {
        this.formItem.manufacturerId = manufacturers[0].id
        this.formItem.manufacturerName = manufacturers[0].name
        this.formItem.manufacturerPerson = manufacturers[0].person
        this.formItem.manufacturerPhone = manufacturers[0].phone
      }

      this.manucacturerSelect = false
    },
    /** 选择用户按钮 */
    handleUserIconClick() {
      if (!this.formItem.orgId) {
        this.$message({
          type: 'warning',
          message: '请选择使用部门!'
        })
        return
      }
      this.userSelDialogFormVisible = true
    },
    selectUserOk() {
      var users = this.$refs.usersTable.selections
      if (users.length > 1) {
        return this.$message({
          showClose: true,
          message: '只能选择一个用户',
          type: 'error'
        })
      }
      if (users.length < 1) {
        this.formItem.userId = ''
        this.formItem.userName = ''
      } else {
        this.formItem.userId = users[0].username
        this.formItem.userName = users[0].name ? users[0].name : users[0].username
      }
      this.userSelDialogFormVisible = false
    },
    selectUserCancel() {
      this.userSelDialogFormVisible = false
    },
    /** 组织机构赋值 */
    selcetOrg(value) {
      this.formItem.orgName = value.title
    },
    inputOrg(value) {
      if (this.isUpdateInit) {
        this.formItem.userId = ''
        this.formItem.userName = ''
      }
      this.isUpdateInit = true
      if (value === undefined) {
        this.formItem.orgName = ''
      }
    },
    /** 选择维保负责人 */
    handleMaintenanceIconClick() {
      this.mainteanceSelDialogFormVisible = true
    },
    selectMainteanceOk() {
      var mainteanceUsers = this.$refs.usersMainteance.selections
      if (mainteanceUsers.length > 1) {
        return this.$message({
          showClose: true,
          message: '只能选择一个用户',
          type: 'error'
        })
      }
      if (mainteanceUsers.length < 1) {
        this.formItem.maintenanceUserName = ''
        this.formItem.maintenanceUserId = ''
      } else {
        this.formItem.maintenanceUserId = mainteanceUsers[0].username
        this.formItem.maintenanceUserName = mainteanceUsers[0].name ? mainteanceUsers[0].name : mainteanceUsers[0].username
      }
      this.mainteanceSelDialogFormVisible = false
    },
    selectMainteanceCancel() {
      this.mainteanceSelDialogFormVisible = false
    },
    /** 合同信息 */
    handleContractClick() {
      this.contractFormVisible = true
    },
    selectContractOk() {
      var selectContract = this.$refs.contract.selections
      console.log(selectContract)
      if (selectContract.length > 1) {
        return this.$message({
          showClose: true,
          message: '只能选择一个合同',
          type: 'error'
        })
      }
      if (selectContract.length < 1) {
        this.formItem.contractName = ''
        this.formItem.contractCode = ''
        this.formItem.contract['manufacturerName'] = ''
      } else {
        this.formItem.contract['manufacturerName'] = selectContract[0].manufacturerName
        this.formItem.contractName = selectContract[0].name
        this.formItem.contractCode = selectContract[0].code
      }
      this.contractFormVisible = false
    },
    selectContractCancel() {
      this.contractFormVisible = false
    },
    // 产品选择OK
    selectProductOk() {
      const selections = this.$refs.type.selections
      if (selections.length !== 1) {
        return this.$message({
          showClose: true,
          message: '只能选择一个标准化产品',
          type: 'error'
        })
      } else {
        this.formItem.productId = selections[0].id
        this.formItem.name = selections[0].name
        this.formItem.money = selections[0].money
        this.formItem.measureUnit = selections[0].measureUnit
        this.formItem.productName = selections[0].specification
        this.productSelDialogFormVisible = false
      }
    },
    // 产品选择取消
    selectProductCancel() {
      this.productSelDialogFormVisible = false
    },
    scrap() {

    }
  }
}
</script>
